package org.fixies.spreadsheet;

import org.fixies.utils.MapIterator;

import java.util.*;


/**
 * ブック
 *
 * @author Kawasaki
 */
public class Book implements Iterable<Sheet>, Element {

    private final String bookName_;

    private final Map<String, Sheet> sheets_;

    public Book(String bookName, Map<String, Sheet> sheets) {
        bookName_ = bookName;
        sheets_ = Collections.unmodifiableMap(sheets);
    }

    public String getId() {
        return bookName_;
    }

    public Iterator<Sheet> iterator() {
        return new MapIterator<String, Sheet>(sheets_);
    }

    public String getMetaData(String key) {
        throw new UnsupportedOperationException("Sorry.." + key);
    }

    public String getRequiredMetaData(String key) {
        throw new UnsupportedOperationException("Sorry.." + key);
    }

    public int size() {
        return sheets_.size();
    }

    /**
     * シートを取得する。
     *
     * @param sheetName シート名
     * @return シート
     */
    public Sheet getSheet(String sheetName) {
        return sheets_.get(sheetName);
    }

    public List<Sheet> getSheetStartWith(String prefix) {
        List<Sheet> result = new ArrayList<Sheet>();
        for (String sheetName : sheets_.keySet()) {
            if (sheetName.startsWith(prefix)) {
                result.add(sheets_.get(sheetName));
            }
        }
        return result;
    }

    public String getOrigin() {
        return "Book=[" + bookName_ + "]";
    }
}
